package com.baogu.database.controller;

import com.alibaba.druid.util.StringUtils;
import com.baogu.database.service.SensorDataImportService;
import com.baogu.database.service.SensorDataService;
import com.baogu.model.common.HttpResult;
import com.baogu.model.database.SensorData;
import com.baogu.model.database.SensorDataImport;
import com.baogu.model.database.where.SensorDataImportWhere;
import com.baogu.model.database.where.SensorDataWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("sensordata")
@RequestMapping("/sensordata")
public class SensordataController {

    @Autowired
    SensorDataService sensorDataService;
    @Autowired
    SensorDataImportService sensorDataImportService;


    @GetMapping("/getNewSensorData")
    public HttpResult getNewSensorData(){
        SensorDataImportWhere where = new SensorDataImportWhere();
        where.setImportState(0);
        List<SensorDataImport> list = sensorDataImportService.allList(where);
        return HttpResult.OK(list);
    }

    @GetMapping("/completeImpSensorData/{vsion}")
    public HttpResult completeImpSensorData(@PathVariable("vsion") String vsion){
        if(StringUtils.isEmpty(vsion)){
            return HttpResult.ERROR("1100","参数错误");
        }
        SensorDataImportWhere where = new SensorDataImportWhere();
        where.setVersion(vsion);
        where.setImportState(1);
        Integer result = sensorDataImportService.updataDataImport(where);
        if(result > 0){
            return HttpResult.OK(null);
        }else{
            HttpResult httpResult = new HttpResult();
            httpResult.setMsg("10001");
            httpResult.setMsg("update fail");
            return httpResult;
        }
    }

    @GetMapping("getSensorDataListByVsion/{vsion}/{tableDate}")
    public HttpResult getSensorDataListByVsion(@PathVariable("vsion") String vsion,@PathVariable("tableDate")String tableDate){
        if(StringUtils.isEmpty(tableDate)){
            return HttpResult.ERROR("1100","参数错误");
        }
        if(StringUtils.isEmpty(vsion)){
            return HttpResult.ERROR("1100","参数错误");
        }
        SensorDataWhere where = new SensorDataWhere();
        where.setVersion(vsion);
        where.setTableDate(tableDate);
        List<SensorData> list = sensorDataService.allList(where);
        return HttpResult.OK(list);
    }
}
